package demo;

import org.apache.log4j.Logger;
import org.opengauss.jdbc.PgConnection;
import org.opengauss.quickautobalance.Cluster;
import org.opengauss.quickautobalance.ConnectionInfo;
import org.opengauss.quickautobalance.ConnectionManager;
import org.opengauss.util.GT;
import testcase.QuickAutoBalanceTest;

import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 */
public class SetParamsTest {
    private static Logger LOGGER = Logger.getLogger(QuickAutoBalanceTest.class);

    private static void setEnableQuickAutoBalanceTest(String url, String username, String password) {
        // 当enableQuickAutoBalance配置错误时，抛出异常。
        String url1 = url + "&enableQuickAutoBalance=sfsfsfsfsfs";
        try {
            DriverManager.getConnection(url1, username, password);
        } catch (SQLException e) {
            LOGGER.error("参数enableQuickAutoBalance设置错误，堆栈：", e);
        }
        // enableQuickAutoBalance=false时，不开启该功能
        String url2 = url + "&enableQuickAutoBalance=false";
        try {
            PgConnection pgConnection = DriverManager.getConnection(url2, username, password).unwrap(PgConnection.class);
            ConnectionInfo connectionInfo = ConnectionManager.getInstance()
                .getCluster(ConnectionManager.getURLIdentifierFromUrl(url2)).getConnectionInfo(pgConnection);
            LOGGER.info(GT.tr("url: {0}. enableQuickAutoBalance={1}", url2, connectionInfo.isEnableQuickAutoBalance()));
            assert !connectionInfo.isEnableQuickAutoBalance();
        } catch (SQLException e) {
            LOGGER.error("参数enableQuickAutoBalance设置错误，堆栈：", e);
        }
        // enableQuickAutoBalance=true时，开启该功能
        String url3 = url + "&enableQuickAutoBalance=true";
        try {
            PgConnection pgConnection = DriverManager.getConnection(url3, username, password).unwrap(PgConnection.class);
            ConnectionInfo connectionInfo = ConnectionManager.getInstance()
                .getCluster(ConnectionManager.getURLIdentifierFromUrl(url3)).getConnectionInfo(pgConnection);
            LOGGER.info(GT.tr("url: {0}. enableQuickAutoBalance={1}", url3, connectionInfo.isEnableQuickAutoBalance()));
            assert connectionInfo.isEnableQuickAutoBalance();
        } catch (SQLException e) {
            LOGGER.error("参数enableQuickAutoBalance设置错误，堆栈：", e);
        }
    }

    private static void setMaxIdleTimeBeforeTerminalTest(String url, String username, String password) {
        // 当maxIdleTimeBeforeTerminal配置为非负整数时，正常
        String url1 = url + "&enableQuickAutoBalance=true&maxIdleTimeBeforeTerminal=100";
        try {
            PgConnection pgConnection = DriverManager.getConnection(url1, username, password).unwrap(PgConnection.class);
            ConnectionInfo connectionInfo = ConnectionManager.getInstance()
                .getCluster(ConnectionManager.getURLIdentifierFromUrl(url1)).getConnectionInfo(pgConnection);
            LOGGER.info(GT.tr("url: {0}. maxIdleTimeBeforeTerminal={1}", url1, connectionInfo.getMaxIdleTimeBeforeTerminal()));
            assert connectionInfo.getMaxIdleTimeBeforeTerminal() == 100;
        } catch (SQLException e) {
            LOGGER.error("参数enableQuickAutoBalance设置错误，堆栈：", e);
        }
        // maxIdleTimeBeforeTerminal 设置为 -1， 抛出异常。
        String url2 = url + "&enableQuickAutoBalance=true&maxIdleTimeBeforeTerminal=-1";
        try {
            DriverManager.getConnection(url2, username, password);
        } catch (SQLException e) {
            LOGGER.error("参数maxIdleTimeBeforeTerminal设置错误，堆栈：", e);
        }
        // maxIdleTimeBeforeTerminal 设置为 10000000000000000000000000000000000000， 抛出异常。
        String url3 = url + "&enableQuickAutoBalance=true&maxIdleTimeBeforeTerminal=10000000000000000000000000000000000000";
        try {
            DriverManager.getConnection(url3, username, password);
        } catch (SQLException e) {
            LOGGER.error("参数maxIdleTimeBeforeTerminal设置错误，堆栈：", e);
        }
    }

    private static void setMinReservedConPerClusterTest(String url, String username, String password) {
        // minReservedConPerCluster 默认值为0
        {
            String url4 = url + "&enableQuickAutoBalance=true";
            try {
                PgConnection pgConnection = DriverManager.getConnection(url4, username, password).unwrap(PgConnection.class);
                Cluster cluster = ConnectionManager.getInstance()
                    .getCluster(ConnectionManager.getURLIdentifierFromUrl(url4));
                LOGGER.info(GT.tr("url: {0}. minReservedConPerCluster={1}", url4, cluster.getMinReservedConPerCluster()));
                if (cluster.getMinReservedConPerCluster() != 0) {
                    LOGGER.error(GT.tr("minReservedConPerCluster:{0} != 0.", cluster.getMinReservedConPerCluster()));
                    return;
                }
            } catch (SQLException e) {
                LOGGER.error("参数enableQuickAutoBalance设置错误，堆栈：", e);
            }
        }
        // 当 minReservedConPerCluster 配置[0,100]整数时，正常
        {
            String url1 = url + "&enableQuickAutoBalance=true&minReservedConPerCluster=90";
            try {
                PgConnection pgConnection = DriverManager.getConnection(url1, username, password).unwrap(PgConnection.class);
                Cluster cluster = ConnectionManager.getInstance()
                    .getCluster(ConnectionManager.getURLIdentifierFromUrl(url1));
                LOGGER.info(GT.tr("url: {0}. minReservedConPerCluster={1}", url1, cluster.getMinReservedConPerCluster()));
                assert cluster.getMinReservedConPerCluster() == 90;
            } catch (SQLException e) {
                LOGGER.error("参数enableQuickAutoBalance设置错误，堆栈：", e);
            }
        }
        // 当 minReservedConPerCluster 设置为-1时，抛出异常。
        {
            String url2 = url + "&enableQuickAutoBalance=true&minReservedConPerCluster=-1";
            try {
                DriverManager.getConnection(url2, username, password);
            } catch (SQLException e) {
                LOGGER.error("参数 minReservedConPerCluster 设置错误，堆栈：", e);
            }
        }
        // 当 minReservedConPerCluster 设置为200时，抛出异常。
        {
            String url3 = url + "&enableQuickAutoBalance=true&minReservedConPerCluster=200";
            try {
                DriverManager.getConnection(url3, username, password);
            } catch (SQLException e) {
                LOGGER.error("参数 minReservedConPerCluster 设置错误，堆栈：", e);
            }
        }
    }

    private static void setMinReservedConPerDatanodeTest(String url, String username, String password) {
        LOGGER.info("minReservedConPerDatanode 默认值为0时：");
        {
            String url4 = url + "&enableQuickAutoBalance=true&loggerLevel=OFF";
            try {
                PgConnection pgConnection = DriverManager.getConnection(url4, username, password).unwrap(PgConnection.class);
                Cluster cluster = ConnectionManager.getInstance()
                    .getCluster(ConnectionManager.getURLIdentifierFromUrl(url4));
                LOGGER.info(GT.tr("url: {0}. minReservedConPerDatanode={1}", url4, cluster.getMinReservedConPerDatanode()));
                if (cluster.getMinReservedConPerCluster() != 0) {
                    LOGGER.error(GT.tr("minReservedConPerDatanode:{0} != 0.", cluster.getMinReservedConPerDatanode()));
                    return;
                }
            } catch (SQLException e) {
                LOGGER.error("参数 minReservedConPerDatanode 设置错误，堆栈：", e);
            }
        }
        LOGGER.info("当 minReservedConPerDatanode 配置[0,100]整数时，正常:");
        {
            String url1 = url + "&enableQuickAutoBalance=true&minReservedConPerDatanode=90&loggerLevel=OFF";
            try {
                PgConnection pgConnection = DriverManager.getConnection(url1, username, password).unwrap(PgConnection.class);
                Cluster cluster = ConnectionManager.getInstance()
                    .getCluster(ConnectionManager.getURLIdentifierFromUrl(url1));
                LOGGER.info(GT.tr("url: {0}. minReservedConPerDatanode={1}", url1, cluster.getMinReservedConPerDatanode()));
                if (cluster.getMinReservedConPerDatanode() != 90) {
                    LOGGER.error(GT.tr("minReservedConPerDatanode:{0} != 90.", cluster.getMinReservedConPerDatanode()));
                    return;
                }
            } catch (SQLException e) {
                LOGGER.error("参数 minReservedConPerDatanode 设置错误，堆栈：", e);
            }
        }
        LOGGER.info("当 minReservedConPerCluster 设置为-1时，抛出异常:");
        {
            String url2 = url + "&enableQuickAutoBalance=true&minReservedConPerDatanode=-1&loggerLevel=OFF";
            try {
                DriverManager.getConnection(url2, username, password);
            } catch (SQLException e) {
                LOGGER.error("参数 minReservedConPerDatanode 设置错误，堆栈：", e);
            }
        }
        LOGGER.info("当 minReservedConPerCluster 设置为200时，抛出异常:");
        {
            String url3 = url + "&enableQuickAutoBalance=true&minReservedConPerDatanode=200&loggerLevel=OFF";
            try {
                DriverManager.getConnection(url3, username, password);
            } catch (SQLException e) {
                LOGGER.error("参数 minReservedConPerDatanode 设置错误，堆栈：", e);
            }
        }
    }

    public static void main(String[] args) {
        LOGGER.info("Testcase: case1, case2, case3, case4.");
        if (args.length < 4) {
            LOGGER.error("request params: testCaseName url username password.");
            return;
        }
        String url = args[1];
        String username = args[2];
        String password = args[3];
        if ("case1".equals(args[0])) {
            setEnableQuickAutoBalanceTest(url, username, password);
        }
        if ("case2".equals(args[0])) {
            setMaxIdleTimeBeforeTerminalTest(url, username, password);
        }
        if ("case3".equals(args[0])) {
            setMinReservedConPerClusterTest(url, username, password);
        }
        if ("case4".equals(args[0])) {
            setMinReservedConPerDatanodeTest(url, username, password);
        }
    }
}
